/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-21
 * Time: 00:22
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;

using Labb4.Event;

namespace Labb4.Pooman.Cards
{
	/// <summary>
	/// An area on the graphical table, hosting graphical representations of cards
	/// </summary>
	public class CardArea
	{
		#region members
		
		/// <summary>
		/// The area's x coordinate
		/// </summary>
		private int						m_Xpos;
		/// <summary>
		/// The area's y coordinate
		/// </summary>
		private int						m_Ypos;
		/// <summary>
		/// The area's width
		/// </summary>
		private int						m_Width;
		/// <summary>
		/// The area's height
		/// </summary>
		private int						m_Height;
		/// <summary>
		/// Is this area vertical or horizontal? True means vertical, false means horizontal
		/// </summary>
		private bool					m_Vertical;
		/// <summary>
		/// The area's exact position as a string, for use with events
		/// </summary>
		private readonly string			m_AreaPositionEventName;
		
		/// <summary>
		/// For optimization
		/// </summary>
		private Event.EventHandler		m_EventHandler;
		/// <summary>
		/// For optimization
		/// </summary>
		private EventManager			m_EventManager;
		
		#endregion
		
		#region constructors
		
		/// <summary>
		/// Constructor, initializes members
		/// </summary>
		/// <param name="xpos">The area's x coordinate</param>
		/// <param name="ypos">The area's y coordinate</param>
		/// <param name="width">The area's width</param>
		/// <param name="height">The area's height</param>
		/// <param name="vertical">Set to true if the area is a vertical area, to false if it's horizontal</param>
		/// <param name="areaPositionEventName">The exact name of the exact position of the area</param>
		public CardArea(int xpos, int ypos, int width, int height, bool vertical, string areaPositionEventName)
		{
			m_Xpos = xpos;
			m_Ypos = ypos;
			m_Width = width;
			m_Height = height;
			m_Vertical = vertical;
			m_AreaPositionEventName = areaPositionEventName;
			
			m_EventHandler = Event.EventHandler.Instance;
			m_EventManager = EventManager.Instance;
		}
		
		#endregion
		
		#region methods
		
		/// <summary>
		/// Positions the card's in the area, evenly spacing them out, but with a maximum distance between the cards
		/// </summary>
		/// <param name="cards">The cards to position in the area</param>
		public void PositionCards(CardPile cards)
		{
			int c = cards.Count;
			
			if(c > 0)
			{
				float x = (float)m_Xpos;
				float y = (float)m_Ypos;
				float spaceX = 0.0f;
				float spaceY = 0.0f;
				
				if(m_Vertical)
				{
					int lastCardsY = m_Height - cards[0].GraphicalRepresentation.Pic.Height;
					spaceY = (float)lastCardsY / (float)(c - 1);
					int totalHeight = -20;
					
					foreach(Card it in cards)
					{
						totalHeight += (it.GraphicalRepresentation.Pic.Height + 20);
					}
					
					if(totalHeight < m_Height)
					{
						y = (float)(m_Ypos + ((m_Height - totalHeight) / 2));
						spaceY = cards[0].GraphicalRepresentation.Pic.Height + 20.0f;
					}
				}
				else
				{
					int lastCardsX = m_Width - cards[0].GraphicalRepresentation.Pic.Width;
					spaceX = (float)lastCardsX / (float)(c - 1);
					int totalWidth = -20;
					
					foreach(Card it in cards)
					{
						totalWidth += (it.GraphicalRepresentation.Pic.Width + 20);
					}
					
					if(totalWidth < m_Width)
					{
						x = (float)(m_Xpos + ((m_Width - totalWidth) / 2));
						spaceX = cards[0].GraphicalRepresentation.Pic.Width + 20.0f;
					}
				}
				
				foreach(Card it in cards)
				{
					it.GraphicalRepresentation.PositionCard((int)x, (int)y, this);
					y += spaceY;
					x += spaceX;
				}
			}
		}
		
		/// <summary>
		/// Plays the event of having won cards in this graphical region
		/// </summary>
		/// <param name="name">The first half of the event's name</param>
		public void PlayWonCardsEvent(string name)
		{
			m_EventHandler.AddEventAndPlay(m_EventManager.GetEvent(name + m_AreaPositionEventName + ".txt"));
		}
		
		/// <summary>
		/// Plays the event of having picked up cards in this graphical region
		/// </summary>
		/// <param name="name">The first half of the event's name</param>
		public void PlayPickedUpCardsEvent(string name)
		{
			m_EventHandler.AddEventAndPlay(m_EventManager.GetEvent(name + m_AreaPositionEventName + ".txt"));
		}
		
		#endregion
		
		#region properties
		
		/// <summary>
		/// The area's x coordinate (set/get)
		/// </summary>
		public int Xpos
		{
			get
			{
				return m_Xpos;
			}
			
			set
			{
				m_Xpos = value;
			}
		}
		
		/// <summary>
		/// The area's y coordinate (set/get)
		/// </summary>
		public int Ypos
		{
			get
			{
				return m_Ypos;
			}
			
			set
			{
				m_Ypos = value;
			}
		}
		
		/// <summary>
		/// The area's width (set/get)
		/// </summary>
		public int Width
		{
			get
			{
				return m_Width;
			}
			
			set
			{
				m_Width = value;
			}
		}
		
		/// <summary>
		/// The area's height (set/get)
		/// </summary>
		public int Height
		{
			get
			{
				return m_Height;
			}
			
			set
			{
				m_Height = value;
			}
		}
		
		/// <summary>
		/// The area's direction, vertical or horizontal. True means vertical, false means horizontal (set/get)
		/// </summary>
		public bool Vertical
		{
			get
			{
				return m_Vertical;
			}
			
			set
			{
				m_Vertical = value;
			}
		}
		
		#endregion
	}
}
